function Categories(categoryList) {
   this.categoryList = categoryList;
}


Categories.prototype.addCategory = function(scheme,term,value) {
   var item = document.createElementNS(XUL_NS,"richlistitem");
   item.setAttribute("class","category-item")
   var lbracket = document.createElementNS(XUL_NS,"label");
   lbracket.setAttribute("value","{");
   item.appendChild(lbracket);
   item.addEventListener('click',function() {
      item.focus();
   },true);

   var schemeE = document.createElementNS(XUL_NS,"textbox");
   schemeE.setAttribute("class","plain");
   schemeE.setAttribute("hidden","true");
   if (scheme) {
      schemeE.setAttribute("value",scheme);
      schemeE.setAttribute("size",""+(scheme.length+1));
   } else {
      schemeE.setAttribute("size","1");
   }
   schemeE.setAttribute("oninput","this.doInput();");
   schemeE.setAttribute("flex","0");
   item.appendChild(schemeE);

   var schemeDisplayE = document.createElementNS(XUL_NS,"label");
   schemeDisplayE.setAttribute("class","plain");
   if (scheme) {
      schemeDisplayE.setAttribute("value",scheme);
   } else {
      schemeDisplayE.setAttribute("value"," ");
   }
   item.appendChild(schemeDisplayE);
   
   this._attachChangeHandlers(schemeE,schemeDisplayE);

   var rbracket = document.createElementNS(XUL_NS,"label");
   rbracket.setAttribute("value","}");
   item.appendChild(rbracket);

   var termDisplayE = document.createElementNS(XUL_NS,"label");
   termDisplayE.setAttribute("class","plain");
   termDisplayE.setAttribute("value",term);
   
   var termE = document.createElementNS(XUL_NS,"textbox");
   termE.setAttribute("value",term);
   termE.setAttribute("class","plain");
   termE.setAttribute("hidden","true");
   termE.setAttribute("size",""+(term.length+1));
   termE.setAttribute("oninput","this.doInput();");
   termE.setAttribute("flex","0");
   item.appendChild(termE);
   item.appendChild(termDisplayE);
   
   this._attachChangeHandlers(termE,termDisplayE);

   var vequals = document.createElementNS(XUL_NS,"label");
   vequals.setAttribute("value",' = "');
   item.appendChild(vequals);

   var valueE = document.createElementNS(XUL_NS,"textbox");
   valueE.setAttribute("value",value ? value : "");
   valueE.setAttribute("class","plain");
   valueE.setAttribute("hidden","true");
   valueE.setAttribute("size",""+(value ? value.length+1 : 1));
   valueE.setAttribute("oninput","this.doInput();");
   valueE.setAttribute("flex","0");
   item.appendChild(valueE);

   var valueDisplayE = document.createElementNS(XUL_NS,"label");
   valueDisplayE.setAttribute("class","plain");
   valueDisplayE.setAttribute("value",value ? value : " ");
   item.appendChild(valueDisplayE);
   
   this._attachChangeHandlers(valueE,valueDisplayE);

   var endLit = document.createElementNS(XUL_NS,"label");
   endLit.setAttribute("value",value && value.length>0 ? '"' : '" (no value)');
   item.appendChild(endLit);
   
   valueE.onchange = function() {
      endLit.setAttribute("value",this.value.length>0 ? '"' : '" (no value)');
   };
   
   this.categoryList.appendChild(item);
}

Categories.prototype._attachChangeHandlers = function(inputE,inputDisplayE) {
   inputE.doInput = function() {
      inputE.size = inputE.value.length+1;
      if (this.timer) {
         clearTimeout(this.timer);
         this.timer = null;
      }
      var current = this;
      this.timer = setTimeout(function() {
         current.onblur();
      },20*1000);
   };
   inputDisplayE.onclick = function() {
      inputDisplayE.hidden = true;
      inputE.hidden = false;
      inputE.inputField.onblur = function() {
         inputDisplayE.value = inputE.value.length>0 ? inputE.value : " ";
         inputE.hidden = true;
         inputDisplayE.hidden = false;
         if (inputE.timer) {
            clearTimeout(inputE.timer);
            inputE.timer = null;
         }
      };
      inputE.inputField.focus();

      var current = inputE;
      inputE.timer = setTimeout(function() {
         current.inputField.onblur();
      },20*1000);
   };
}

Categories.prototype.deleteSelected = function() {
   var index = this.categoryList.selectedIndex;
   var item = this.categoryList.selectedItem;

   this.categoryList.removeChild(item);
}

Categories.prototype.getCategories = function() {
   var set = [];
   var len = this.categoryList.getRowCount();
   for (var i=0; i<len; i++) {
      var item = this.categoryList.getItemAtIndex(i);
      var nl = item.getElementsByTagNameNS(XUL_NS,"textbox");
      var scheme = nl.item(0).value+"";
      scheme.trim();
      var term = nl.item(1).value+"";
      term.trim();
      var value = nl.item(2).value;
      if (value.length==0) {
         value = null;
      }
      if (term.length>0) {
         set.push({ scheme: scheme, term: term, value: value});
       }
   }
   return set;
} 

Categories.prototype.clear = function() {
   var len = this.categoryList.getRowCount();
   for (var i=0; i<len; i++) {
      this.categoryList.removeChild(this.categoryList.getItemAtIndex(0));
   }
}
